<template><div><h2 id="扩展基本使用" tabindex="-1"><a class="header-anchor" href="#扩展基本使用"><span>扩展基本使用</span></a></h2>
<h2 id="基本使用" tabindex="-1"><a class="header-anchor" href="#基本使用"><span>基本使用</span></a></h2>
<h3 id="扩展安装" tabindex="-1"><a class="header-anchor" href="#扩展安装"><span>扩展安装</span></a></h3>
<p><code v-pre>Dcat Admin</code>中扩展支持以下三种安装方式，安装成功后均能在扩展管理页面<code v-pre>admin/auth/extensions</code>看到相关扩展信息</p>
<h4 id="_1-通过应用市场安装" tabindex="-1"><a class="header-anchor" href="#_1-通过应用市场安装"><span>1.通过应用市场安装</span></a></h4>
<p>敬请期待…</p>
<h4 id="_2-本地安装" tabindex="-1"><a class="header-anchor" href="#_2-本地安装"><span>2.本地安装</span></a></h4>
<p>下载扩展的<code v-pre>zip</code>压缩包，注意必须是<code v-pre>zip</code>格式，然后打开扩展管理页面<code v-pre>admin/auth/extensions</code>，然后点击表格工具栏的<code v-pre>本地安装</code>按钮上传提交即可。</p>
<h4 id="_3-composer安装" tabindex="-1"><a class="header-anchor" href="#_3-composer安装"><span>3.composer安装</span></a></h4>
<p>根据扩展开发者文档提供的说明，直接使用composer安装即可</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require <span class="token punctuation">{</span>扩展名称<span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="启用扩展" tabindex="-1"><a class="header-anchor" href="#启用扩展"><span>启用扩展</span></a></h4>
<p>安装之后，需要在扩展管理页面点击 <code v-pre>更新至xxx版本</code> 以及更新 <code v-pre>启用</code> 按钮之后方可正常使用</p>
<h2 id="开发扩展" tabindex="-1"><a class="header-anchor" href="#开发扩展"><span>开发扩展</span></a></h2>
<p>详细的开发教程，请参考文档 <a href="https://learnku.com/docs/dcat-admin/2.x/basic-use/8128" target="_blank" rel="noopener noreferrer">开发扩展</a> 章节。</p>
<h3 id="服务注册与初始化" tabindex="-1"><a class="header-anchor" href="#服务注册与初始化"><span>服务注册与初始化</span></a></h3>
<blockquote>
<p>{tip} 如果你对服务提供者的概念并不熟悉，请先前往<a href="https://learnku.com/docs/laravel/8.x/providers/9362" target="_blank" rel="noopener noreferrer">Laravel文档 - 服务提供者</a>学习。</p>
</blockquote>
<p>扩展的 <code v-pre>ServiceProvider</code> 类实际上是一个<a href="https://learnku.com/docs/laravel/8.x/providers/9362" target="_blank" rel="noopener noreferrer">服务提供者</a>，唯一的区别是扩展的 <code v-pre>ServiceProvider</code> 不能重写 <code v-pre>boot</code> 方法，需要通过 <code v-pre>init</code> 代替 <code v-pre>boot</code>方法。</p>
<h3 id="版本管理" tabindex="-1"><a class="header-anchor" href="#版本管理"><span>版本管理</span></a></h3>
<p>每个扩展都有一个<code v-pre>version.php</code>文件，通过这个文件可以实现版本管理功能，每次发布新版本我们只需要往这个文件添加新的版本号以及相关描述即可</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">// key 是版本号，注意这里不要带 v 前缀！</span></span>
<span class="line">    <span class="token string single-quoted-string">'1.0.0'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'版本描述信息，可以有多条'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'描述2...'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'create_operation_log.php'</span><span class="token punctuation">,</span> <span class="token comment">// 版本迁移文件，可以有多条</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token string single-quoted-string">'1.0.1'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'版本描述信息，可以有多条'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'update_operation_log.php'</span><span class="token punctuation">,</span> <span class="token comment">// 版本迁移文件</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token operator">...</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="升级版本" tabindex="-1"><a class="header-anchor" href="#升级版本"><span>升级版本</span></a></h4>
<p>安装了新的版本代码之后，可以在扩展管理页面<code v-pre>admin/auth/extensions</code>中点击更新按钮进行升级。</p>
<p>点击升级后如果有迁移文件则会运行迁移文件，如果有菜单则会重新创建菜单，如果有静态资源则会自动重新发布资源文件。</p>
<h4 id="回滚版本" tabindex="-1"><a class="header-anchor" href="#回滚版本"><span>回滚版本</span></a></h4>
<p>通过命令 <code v-pre>php artisan admin:ext-rollback {扩展名称} {版本号}</code> 可以回滚到指定版本，但需要注意的是，回滚扩展会删除数据，可能会导致数据丢失，请谨慎操作！！！</p>
<h4 id="卸载" tabindex="-1"><a class="header-anchor" href="#卸载"><span>卸载</span></a></h4>
<p>如果你的扩展已安装，通过扩展页面 <code v-pre>admin/auth/extensions</code> 可以扩展进行卸载，但需要注意的是，卸载扩展会删除数据，可能会导致数据丢失，请谨慎操作！！！</p>
<p>如果你想完全移除扩展的代码，则直接删除 <code v-pre>dcat-admin-extensions</code> 目录下对应的扩展文件夹即可。</p>
<h3 id="视图-view" tabindex="-1"><a class="header-anchor" href="#视图-view"><span>视图 (view)</span></a></h3>
<p>视图的默认目录为<code v-pre>扩展目录/resources/view</code></p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">├── resources </span>
<span class="line">│   ├── <span class="token punctuation">..</span>.</span>
<span class="line">│   └── views <span class="token comment"># 视图目录</span></span>
<span class="line">│       └── index.blade.php <span class="token comment"># 视图示例文件</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>只要把视图文件放在上述目录，系统就会自动给视图目录注册别名，别名与扩展名称相同。假设你的扩展包名称为 <code v-pre>dcat-admin/form-step</code>，则可以通过以下方式加载视图</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'dcat-admin.form-step::index'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="静态资源" tabindex="-1"><a class="header-anchor" href="#静态资源"><span>静态资源</span></a></h3>
<p>假设你的扩展包名称为 <code v-pre>dcat-admin/form-step</code>，如果你的扩展中包含静态资源如下，那么你可以通过 <code v-pre>FormStepServiceProvider</code> 上的 <code v-pre>$js</code> 和 <code v-pre>$css</code> 属性为你的静态资源注册别名</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">└── resources </span>
<span class="line">    └─── assets</span>
<span class="line">      ├── css</span>
<span class="line">      │   └── index<span class="token operator">.</span>css</span>
<span class="line">      └── js</span>
<span class="line">          └── index<span class="token operator">.</span>js</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">FormStepServiceProvider</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceProvider</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">protected</span> <span class="token variable">$js</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'js/index.js'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">protected</span> <span class="token variable">$css</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'css/index.css'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>然后就可以通过下面的方法加载静态资源</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Dcat<span class="token punctuation">\</span>Admin<span class="token punctuation">\</span>Admin</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 直接用你的 包名 即可引入扩展包的静态资源</span></span>
<span class="line"><span class="token class-name static-context">Admin</span><span class="token operator">::</span><span class="token function">requireAssets</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'@dcat-admin.form-step'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当然你也可以不通过 <code v-pre>$js</code> 和 <code v-pre>$css</code> 属性注册别名，那么也可以用下面的方法直接加载静态资源，效果是一样的</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 上面的写法相当于</span></span>
<span class="line"><span class="token class-name static-context">Admin</span><span class="token operator">::</span><span class="token function">js</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'@dcat-admin.form-step/js/index.js'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token class-name static-context">Admin</span><span class="token operator">::</span><span class="token function">css</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'@dcat-admin.form-step/css/index.css'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="判断扩展是否启用" tabindex="-1"><a class="header-anchor" href="#判断扩展是否启用"><span>判断扩展是否启用</span></a></h3>
<p>判断扩展是否启用：<code v-pre>Admin::extension()-&gt;enabled('package.name')</code>，当扩展不存在或禁用时，返回 <code v-pre>false</code>；当扩展启用的时返回 <code v-pre>true</code>。</p>
</div></template>


